package com.zx.work1.servlet;

import com.zx.work1.bean.Student;
import com.zx.work1.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "queryStudentNumServlet", urlPatterns = "/queryStudentNum")
public class queryStudentNumServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        // 1.获取请求中的用户名和密码信息并打印出来
        String studentNum = request.getParameter("studentNum");
        System.out.println("获取到的学号为：" + studentNum);

        StudentService studentService = new StudentService();
        Student student = studentService.queryStudentNumServeiece(Integer.valueOf(studentNum));
        if(student == null){
            System.out.println("查询学员失败");
            request.setAttribute("error", "查询学员失败，学号不存在！");
            // 实现服务器跳转  共享request和response对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("main.jsp");
            requestDispatcher.forward(request, response);
            return;
        }
        request.getSession().setAttribute("queryStudent", student);
        if(student.getNum() != 0){
            response.sendRedirect("queryStudent.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
